﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bluyel.Core
{
    public class ValueBuilder
    {
        FactoryMgrBase _factoryMgr;

        TypeMgrBase _typeMgr;

        TypeFinderBase _typeFinder;

        //=====================================================================
        // Constructor.

        //---------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="factory"></param>
        public ValueBuilder(FactoryMgrBase factoryMgr, TypeMgrBase typeMgr, TypeFinderBase typeFinder)
        {
            _factoryMgr = factoryMgr;
            _typeMgr = typeMgr;

            _typeFinder = typeFinder;
        }

        //=====================================================================
        // Public Check methods.

        //---------------------------------------------------------------------
        /// <summary>
        /// Check the syntax of the var name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool CheckVarName(string name)
        {
            if (name == null)
                return false;

            // check that the name do not contains special characters
            // TODO:

            return true;
        }

        //=====================================================================
        // Public Create Value type basic methods.

        //---------------------------------------------------------------------
        /// <summary>
        /// Create a (empty) data value, based on the type.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public IValue CreateValue(ITypeBasic type)
        {
            return _factoryMgr.FactoryValue.CreateValue(type);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Create an int value.
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public ValueInt CreateValueInt(int val)
        {
            // check that the type int exists
            if (_typeFinder.GetTypeInt() == null)
                return null;

            return _factoryMgr.FactoryValue.CreateValueInt(val);
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Create a long value.
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public ValueLong CreateValueLong(int val)
        {
            // check that the type int exists
            if (_typeFinder.GetTypeLong() == null)
                return null;

            return _factoryMgr.FactoryValue.CreateValueLong(val);
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Create a double data value.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ValueDouble CreateValueDouble(double val)
        {
            // check that the type double exists
            if (_typeFinder.GetTypeDouble() == null)
                return null;

            return _factoryMgr.FactoryValue.CreateValueDouble(val);
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Create a string data value.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ValueString CreateValueString(string val)
        {
            // check that the type string exists
            if (_typeFinder.GetTypeString() == null)
                return null;

            // ok the type string is defined
            return _factoryMgr.FactoryValue.CreateValueString(val);
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Create a bool data value.
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ValueBool CreateValueBool(bool val)
        {
            // check that the type bool exists
            if (_typeFinder.GetTypeBool() == null)
                return null;

            return _factoryMgr.FactoryValue.CreateValueBool(val);
        }


        //=====================================================================
        // Public Create DVal TypePhy methods.

        //---------------------------------------------------------------------
        /// <summary>
        /// Create a DataValue type of PhysQty.
        /// 
        /// </summary>
        /// <param name="typePhysQty"></param>
        /// <returns></returns>
        public IValuePhy CreateValue(ITypePhy typePhysQty)
        {
            return _factoryMgr.FactoryValue.CreateValue(typePhysQty);
        }

        //---------------------------------------------------------------------
        public ValuePhyLength CreateValuePhyLength()
        {
            return (ValuePhyLength)_factoryMgr.FactoryValue.CreateValue(_typeFinder.GetTypePhyLength());
        }

        //---------------------------------------------------------------------
        public ValuePhyLength CreateValuePhyLength(double val, TypePhyLengthPrefix prefix)
        {
            ValuePhyLength dv = CreateValuePhyLength();
            if (!SetVal(dv, val, prefix))
                return null;

            return dv;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// set a value and a prefix/multiple, to the dataValue type physQty Length.
        /// </summary>
        /// <param name="dvLength"></param>
        /// <param name="val"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public bool SetVal(ValuePhyLength dvLength, double val, TypePhyLengthPrefix prefix)
        {
            if (dvLength == null)
                return false;
            dvLength.Value= val;
            dvLength.Prefix= prefix;
            return true;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// set a value to the dataValue type physQty Length.
        /// </summary>
        /// <param name="dvLength"></param>
        /// <param name="val"></param>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public bool SetVal(ValuePhyLength dvLength, double val)
        {
            if (dvLength == null)
                return false;
            dvLength.Value= val;
            return true;
        }
    }
}
